﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

using FrbaBus.Core;

namespace FrbaBus.Abm_Recorrido
{
    public partial class RecorridoModificacion2 : Form
    {
        public RecorridoModificacion2()
        {
            InitializeComponent();
        }

        private RecorridoModificacion1 parent = null;
        public RecorridoModificacion2(Form callingForm)
        {
            parent = callingForm as RecorridoModificacion1;

            InitializeComponent();
        }


        // CONTROLES DE INGRESOS DE DATOS EN TEXTBOX NUMERICOS
        private void Numerical_TextBox_Check(object sender, KeyPressEventArgs e)
        {
            // only allow numerical digits
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }
            // only allow one decimal point
            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
            // oly allow 2 decimal digits
            if (Regex.IsMatch(numericUpDown1.Text, @"\.\d\d") && !char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void Numerical_TextBox_Check2(object sender, KeyPressEventArgs e)
        {
            // only allow numerical digits
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }
            // only allow one decimal point
            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
            // oly allow 2 decimal digits
            if (Regex.IsMatch(numericUpDown2.Text, @"\.\d\d") && !char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        public bool origen = false;

        private void button1_Click(object sender, EventArgs e)
        {
            origen = true;
            Abm_Recorrido.BusquedaCiudad frm = new Abm_Recorrido.BusquedaCiudad(this);
            frm.ShowDialog(this);
        }



        private void button2_Click(object sender, EventArgs e)
        {
            origen = false;
            Abm_Recorrido.BusquedaCiudad frm = new Abm_Recorrido.BusquedaCiudad(this);
            frm.ShowDialog(this);
        }

        public class Foo
        {
            public static int validacion = 1;
        }

        private void RecorridoModificacion2_Load(object sender, EventArgs e)
        {
            

            comboBox3.Items.Clear();
            comboBox3.Text = "";
            label7.Visible = false;
            label8.Visible = false;
          //  comboBox3
            DBManager db = new DBManager();
            SqlCommand consulta = new SqlCommand("select Tipo_Servicio from ALL_IN.Tipo_Servicio");
            SQLResponse r = db.executeQuery(consulta);
            SqlCommand consulta2 = new SqlCommand("select count (*) from ALL_IN.Tipo_Servicio");
            SQLResponse r2 = db.executeQuery(consulta2);
            int valor = (int)r2.result.Rows[0][0];
            for (int i = 0; i < valor; i++)
            {
                comboBox3.Items.Add(r.result.Rows[i][0].ToString());
            }
            textBox5.Text = String.Copy(this.parent.Row.Cells[0].Value.ToString());
            textBox3.Text = String.Copy(this.parent.Row.Cells[1].Value.ToString());
            textBox4.Text = String.Copy(this.parent.Row.Cells[2].Value.ToString());
            comboBox3.Text = String.Copy(this.parent.Row.Cells[3].Value.ToString());
            numericUpDown1.Text = String.Copy(this.parent.Row.Cells[4].Value.ToString());
            numericUpDown2.Text = String.Copy(this.parent.Row.Cells[5].Value.ToString());
            textBox5.ReadOnly = true;
            this.parent.Row.Cells[0].Dispose();
            this.parent.Row.Dispose();
            SqlCommand consulta1 = new SqlCommand("select ALL_IN.existeViaje5('" + textBox5.Text + "')");
            SQLResponse r1 = db.executeQuery(consulta1);
            if (r1.result.Rows[0][0].ToString() == "True" )
            {
                comboBox3.Enabled = false;
                textBox5.ReadOnly = true;
                textBox3.ReadOnly = true;
                textBox4.ReadOnly = true;
                button1.Enabled = false;
                button2.Enabled = false;
                label7.Visible = true;
                label8.Visible = true;
            }

        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            
            // VALIDAR QUE HAY DATOS EN TODOS LOS CAMPOS
            DBManager db = new DBManager();
            Foo.validacion = 1;
            if (textBox3.Text == "" || textBox4.Text == "" || comboBox3.Text == "" || numericUpDown1.Text == "" || numericUpDown2.Text == "")
            {
                MessageBox.Show("Todos los campos deben ser completados");
                Foo.validacion = 0;
            }

            // VALIDAR QUE EL ORIGEN ES UNA CIUDAD VALIDA
            if (textBox3.Text != "" )
            {
                SqlCommand consulta5 = new SqlCommand("select ALL_IN.existeCiudadHabilitada('" + textBox3.Text + "')");
                SQLResponse r5 = db.executeQuery(consulta5);

                if (r5.result.Rows[0][0].ToString() == "False")
                {
                    MessageBox.Show("Seleccione una ciudad valida para el origen");
                    Foo.validacion = 0;
                }
            }

            // VALIDAR QUE EL DESTINO ES UNA CIUDAD VALIDA
            if (textBox4.Text != "" )
            {
                SqlCommand consulta6 = new SqlCommand("select ALL_IN.existeCiudadHabilitada('" + textBox4.Text + "')");
                SQLResponse r6 = db.executeQuery(consulta6);
                if (r6.result.Rows[0][0].ToString() == "False")
                {
                    MessageBox.Show("Seleccione una ciudad valida para el destino");
                    Foo.validacion = 0;
                }
            }

            if (Foo.validacion == 1)
            {
                SqlCommand consulta3 = new SqlCommand("UPDATE ALL_IN.Recorrido set Origen='" + textBox3.Text + "', Destino='" + textBox4.Text +
                    "', Precio_Pasaje_Base=@Precio_Pasaje_Base, Precio_Kg_Base=@Precio_Kg_Base, Tipo_Servicio='"
                    + comboBox3.Text + "' WHERE ID_Recorrido='" + textBox5.Text + "'");
                  
                consulta3.Parameters.Add("@Precio_Pasaje_Base", SqlDbType.Decimal)
             .Value = System.Convert.ToDecimal(numericUpDown1.Text);
                consulta3.Parameters.Add("@Precio_Kg_Base", SqlDbType.Decimal)
             .Value = System.Convert.ToDecimal(numericUpDown2.Text);
                db.executeQuery(consulta3);
                this.Close();

             }

        }



    }
}
